#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include "node.h"
#include "event.h"
#include "display.h"
#include "timer.h"
#include "init.h"
#include "debug_printf.h"
#include "net.h"

/****************************************
 * Autor: Joilnen Batista Leite IC/UFAL *
 ****************************************/

Node *node = NULL;

static int height;
static int width;
static int ang, aspect;

Socket *sock = NULL;

static void reshape(int w, int h) {
	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	// glOrtho(-w/2.0, w/2.0, -h/2.0, h/2.0, -200.0, 200.0);
	gluPerspective(ang, w/h, 0.1, 5000);
	glMatrixMode(GL_MODELVIEW);

	// Seta camera
	glLoadIdentity();
	node->cam[0] = 0;
	node->cam[1] = 0;
	node->cam[2] = 1000;
	node->view_point[0] = \
	node->view_point[1] = \
	node->view_point[2] = 0;
	gluLookAt(node->cam[0], node->cam[1], node->cam[2],
		  node->view_point[0], node->view_point[1], node->view_point[2],
		  0, 1, 0);
}

static void r(void) {
	if(node->frot && timer[0]->isOver(timer[0])) {
		timer[0]->count++;
		// D("Ok timer ticking\n");
		// glLoadIdentity();
		/***
		gluLookAt(0, 0, -1000,
			  0, 0, 0,
			  0, 1, 0);
		***/
		if(timer[0]->count == 2000)
			timer[0]->count = 0;
		glMatrixMode(GL_MODELVIEW);
		// glRotatef(timer[0]->count, 0.0, 1.0, 0.0);
		glRotatef(5, 0.0, 1.0, 0.0);
		D("Nao tah zerado\n");
	}
	// else
	//	D("Tah zerado\n");
	sock->getMessage(sock); // com accept nao rola acho q tem usar o select
	glutPostRedisplay();
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv); 
	sock = makeSocket();
	sock->init(sock);
	height = glutGet(GLUT_SCREEN_HEIGHT);
	width = glutGet(GLUT_SCREEN_WIDTH);
	aspect = width / height;
	ang = 45;
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(width, height);
	glutInitWindowPosition(0, 0);
	glutCreateWindow("Structures modeling 0.001");

	init();
	timer[0] = makeTimer(DEFAULT);
	timer[0]->set(timer[0], 0);

	glutIdleFunc(r);
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutMainLoop();

	return 0;
}


